# -*- coding:utf-8 -*-

__author__='zhaoxp'

from functools import wraps

'''
使用functools.wraps将会对调试器和对象序列化器带来便利
'''

def trace(func):
    def wrapper(*args,**kwargs):
        result = func(*args, **kwargs)
        print('%s(%r, %r) -> %r'%(func.__name__,args,kwargs,result))
        return result
    return wrapper


@trace
def fibonacci(n):
    '''return the n-th Fibonacci number'''
    if n in (0,1):
        return n
    else:
        return (fibonacci(n-2)+fibonacci(n-1))

################################################################################################

def trace2(func):
    @wraps
    def wrapper(*args,**kwargs):
        result = func(*args, **kwargs)
        print('%s(%r, %r) -> %r'%(func.__name__,args,kwargs,result))
        return result
    return wrapper


def fibonacci2(n):
    '''return the n-th Fibonacci number'''
    if n in (0,1):
        return n
    else:
        return (fibonacci2(n-2)+fibonacci2(n-1))

if __name__=='__main__':
    print('define function decorators with functools.wraps')
    result = fibonacci(3)
    print('fibonacci func name = %s  and is not <func>.__name__'%fibonacci)
    print('help info:')
    print(help(fibonacci))
    print('*'*30)
    fibonacci2(3)
    print('fibonacci2 func name = %s'%fibonacci2)
    print('help info:')
    print(help(fibonacci2))